﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class SystemCountryController : DefaultController
	{
		public SystemCountryController(ICountryService countryService)
		{
			this.CountryService = countryService;
		}

		public ICountryService CountryService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(int? page)
		{
			var countries = CountryService.GetPaged(new CountrySpecification
			{
				Page = page,
				Limit = Settings.Countries.PageLimit
			});

			var country = countries.FirstOrDefault();

			return country.CanView ? View(Views.Index, Masters.System, countries) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var country = CountryService.Create();

			return country.CanCreate ? View(Views.Create, Masters.System, new CountryCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(CountryCreate value)
		{
			try
			{
				var country = CountryService.Create();

				if (!country.CanCreate)
				{
					return NotAuthorized();
				}

				this.CountryService.Insert(country, value);

				return Message(Routes.SystemCountryUpdate, new { id = country.Id }, Resources.Messages.CountryCreated.Fmt(country.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var country = CountryService.GetById(id);

			if (country == null)
			{
				return Error(Resources.Messages.CountryNotFound);
			}

			return country.CanUpdate ? View(Views.Update, Masters.System, new CountryUpdate(country)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(CountryUpdate value)
		{
			try
			{
				var country = CountryService.GetById(value.Id);

				if (country == null)
				{
					return Error(Resources.Messages.CountryNotFound);
				}

				if (!country.CanUpdate)
				{
					return NotAuthorized();
				}

				this.CountryService.Update(country, value);

				return Message(Routes.SystemCountryUpdate, new { id = country.Id }, Resources.Messages.CountryUpdated.Fmt(country.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var country = this.CountryService.GetById(id);

			if (country == null)
			{
				return base.Error(Resources.Messages.CountryNotFound);
			}

			return country.CanDelete ? base.View(Views.Delete, Masters.System, new CountryDelete(country)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(CountryDelete value)
		{
			try
			{
				var country = this.CountryService.GetById(value.Id);

				if (country == null)
				{
					return base.Error(Resources.Messages.CountryNotFound);
				}

				if (!country.CanDelete)
				{
					return NotAuthorized();
				}

				this.CountryService.Delete(country);

				return base.RedirectToRoute(Routes.SystemCountryIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Setup()
		{
			var country = this.CountryService.Create();

			if (!country.CanCreate)
			{
				return NotAuthorized();
			}

			var count = this.CountryService.Count();

			if (count > 0)
			{
				return base.Error(Resources.Messages.CountryAlreadySetup);
			}

			this.CountryService.Setup();

			return base.RedirectToRoute(Routes.SystemCountryIndex);
		}
	}
}